<?php
namespace Home\Controller;
use Think\Controller;
use Think\Verify;
class IndexController extends Controller {
    public function index(){
    	if(IS_POST){
    		echo 'ok';
    	}else{
        	$this->display();
    	}
    }
    
    /*
     * 
     * 浏览图片
     */
    public function liulan(){
    	$tp = D('Index');
    	$map['status'] = 1;
    	$list = $tp->where($map)->select();
    	//dump($list);
    	$this->assign('list',$list);   	
    	$this->display();
    }
    public function liulanwyl(){
    	$tp = D('Index');
    	$list = $tp->select();
    	$statusArr = array("待审核", "已通过");
    	foreach ($list as $k => $v) {
    		$list[$k]['status'] = $statusArr[$v['status']];
    	}    	
    	dump($list[0]['status']);
    	$this->assign('list',$list);
    	$this->display();
    }
    public function changeStatus(){
    	$id = I("get.id");
    	$map['id'] = $id;
    	$status = D('Index')->where($map)->getField('status');
    	$data['status'] = abs($status-1);
    	$statusArr = array("待审核", "已通过");
    	if(D('Index')->where($map)->save($data)){
    		echo json_encode(array("status" => 1, "info" => $statusArr[$data['status']]));
    		//echo '<img src="../Public/Img/action_'.$data['is_recommend'].'.png" border="0">';
    		exit;
    	}
    	return false;
    }
    public function up(){
    	if(IS_POST){
    		//dump (I('post.verify'));
    		if (!$this->check_verify(I('post.verify'))) {
    			$this->error('验证码输入错误！');
    		}
    		if (!I('post.name')) {
    			$this->error('请输入姓名！');
    		}
    		if (!I('post.xykh')) {
    			$this->error('请输入校园卡号！');
    		}    		
    		if (!I('post.mobile')) {
    			$this->error('请输入电话！');
    		}
    		if (I('post.mobile').length<11) {
    			$this->error('电话号码错误！');
    		}
    		$tp = D('Index');
    		$info = $tp->create();
    		if($info){
    			$upload = new \Think\Upload();// 实例化上传类
    			$upload->maxSize = 20*1024*1024 ;// 设置附件上传大小
    			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    			$upload->rootPath = './Uploads/'; // 设置附件上传根目录
    			$upload->savePath = '';
    			$upload->saveName = time().'_'.mt_rand();
    			// 上传单个文件
    			$img   =   $upload->upload();
    			//dump($img);
    			if(!$img) {// 上传错误提示错误信息
    				$this->error($upload->getError());
    			}else{// 上传成功 获取上传文件信息    			
    				$info['tpurl'] = $img['photo']['savepath'].$img['photo']['savename'];
    			}
    			$info['ctime'] = time();
    			$info['status'] = 0;
    		 	$id = $tp->add($info);
	   		 	if($id){
    		 		$this->success('提交成功！');
    		 	}else{
    		 		$this->error('提交失败！');
    		 	}
    		 }else{
    		 	$this->error($tp->getError());
    		 }
    	}else{
			$this->error('提交参数错误！');
    	}
    }
    
    /**
     * 图片验证码生成，用于登录和注册
     * @author 
     */
    public function verify($vid = 1) {
    	$config =    array(
    			    'expire'    =>    200,    // 验证码字体大小
    			    'useNoise'    =>    false, // 关闭验证码杂点
    			);
    	 
    	$verify = new Verify();
    	$verify->length = 4;
    	$verify->entry($vid);
    }
    
    /**
     * 检测验证码
     * @param  integer $id 验证码ID
     * @return boolean 检测结果
     */
    function check_verify($code, $vid = 1) {
    	$verify = new Verify();
    	return $verify->check($code, $vid);
    }
        
}